<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>访问列表</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="__ADMIN_STATIC__/css/oksub.css">
  <script type="text/javascript" src="__STATIC__/lib/loading/okLoading.js"></script>
</head>
<body>
<div class="ok-body">
  <!--模糊搜索区域-->
  <div class="layui-row">
    <form class="layui-form layui-col-md12 ok-search">
      <input class="layui-input" placeholder="开始日期" autocomplete="off" id="startTime" name="start_time">
      <input class="layui-input" placeholder="截止日期" autocomplete="off" id="endTime" name="end_time">
      <button class="layui-btn" lay-submit="" lay-filter="search">
        <i class="layui-icon layui-icon-search"></i>
      </button>
    </form>
  </div>
  <!--数据表格-->
  <table class="layui-hide" id="tableId" lay-filter="tableFilter"></table>
</div>
<!--js逻辑-->
<script src="__STATIC__/lib/layui/layui.js"></script>
<script>
    layui.use(["element", "table", "okLayer", "okUtils", "okMock", "form", "laydate"], function () {
        let table = layui.table;
        let util = layui.util;
        let form = layui.form;
        let laydate = layui.laydate;
        let okLayer = layui.okLayer;
        let okUtils = layui.okUtils;
        okLoading.close();
        util.fixbar({});

        laydate.render({elem: "#startTime", type: "datetime"});
        laydate.render({elem: "#endTime", type: "datetime"});

        let articleTable = table.render({
            elem: "#tableId",
            url: "{:url('statement/visitedList')}",
            limit: 20,
            page: true,
            even: true,
            toolbar: "#toolbarTpl",
            size: "sm",
            cols: [[
                {type: "checkbox", fixed: "left"},
                {field: "id", title: "ID", width: 80},
                {field: "session", title: "SESSID", width: 200},
                {field: "browser", title: "客户端", width: 100},
                {field: "ip", title: "IP", width: 120},
                {field: "visited", title: "浏览次数", width: 80},
                {field: "username", title: "用户", width: 100, templet: "#userTpl"},
                {field: "update_time", title: "创建时间", width: 180},
                {field: "create_time", title: "创建时间", width: 180},
                {title: "操作", width: 60, align: "center", fixed: "right", templet: "#operationTpl"}
            ]],
            done: function (res, curr, count) {
                console.log(res, curr, count)
            }
        });

        form.on("submit(search)", function (data) {
            articleTable.reload({
                where: data.field,
                page: {curr: 1}
            });
            return false;
        });

        //批量删除
        table.on("toolbar(tableFilter)", function (obj) {
            if(obj.event === "batchDel"){
                batchDel();
            }
        });

        //删除
        table.on("tool(tableFilter)", function (obj) {
            let data = obj.data;
            if (obj.event === 'del'){
                del(data.id);
            }
        });


        function batchDel() {
            okLayer.confirm("确定要批量删除吗？", function (index) {
                layer.close(index);
                var idsStr = okUtils.tableBatchCheck(table);
                if (idsStr) {
                    okUtils.ajax("{:url('statement/batchDel')}", "delete", {ids: idsStr}, true).done(function (response) {
                        console.log(response);
                        okUtils.tableSuccessMsg(response.msg);
                    }).fail(function (error) {
                        console.log(error)
                    });
                }
            });
        }

        function del(id) {
            okLayer.confirm("确定要删除吗？", function () {
                okUtils.ajax("{:url('statement/delete')}", "delete", {id: id}, true).done(function (response) {
                    console.log(response);
                    okUtils.tableSuccessMsg(response.msg);
                }).fail(function (error) {
                    console.log(error)
                });
            })
        }
    })
</script>

<script type="text/html" id="userTpl">
  {{#  if(d.username){ }}
  <span>{{ d.username }}</span>
  {{#  } else { }}
  <span> 游客 </span>
  {{#  } }}
</script>

<!-- 行工具栏模板 -->
<script type="text/html" id="operationTpl">
  <a href="javascript:" title="删除" lay-event="del"><i class="layui-icon">&#xe640;</i></a>
</script>


<!-- 头工具栏模板 -->
<script type="text/html" id="toolbarTpl">
  <div class="layui-btn-container">
    <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="batchDel">批量删除</button>
  </div>
</script>

</body>
</html>
